/*
 * Copyright 2017-2021 Pranav Pandey
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pranavpandey.ohos.dynamic.toasts.sample;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.text.Font;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

import java.util.Optional;

/**
 * custom dialog
 *
 * @since 2021-7-27
 */
public class CustomDialog extends CommonDialog implements Component.ClickedListener {
    private Context mContext;
    private Component customComponent;
    private Text websiteView;
    private Text githubView;
    private Text linkView;
    private Text cancelView;
    private Text playView;
    private Text writtenView;
    private Text desView;
    private Intent intent = new Intent();

    /**
     * custom dialog 构造函数
     *
     * @param context 上下文
     */
    public CustomDialog(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void create() {
        super.create();
        customComponent = LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_dialog_about, null, true);
        setContentCustomComponent(customComponent);
        Optional<Display> optional = DisplayManager.getInstance().getDefaultDisplay(mContext);
        int width = optional.get().getRealAttributes().width;
        int height = optional.get().getRealAttributes().height;
        setSize(width - 180, (int) (height * 0.445));
        Text message = (Text) customComponent.findComponentById(ResourceTable.Id_dialog_about_text);
        message.setLineSpacing(0f, 1.2f);
        message.setFont(Font.DEFAULT);
        setAlignment(LayoutAlignment.CENTER);
        setAutoClosable(true);

        websiteView = (Text) customComponent.findComponentById(ResourceTable.Id_url_one);
        websiteView.setClickedListener(this);
        githubView = (Text) customComponent.findComponentById(ResourceTable.Id_url_two);
        githubView.setClickedListener(this);
        linkView = (Text) customComponent.findComponentById(ResourceTable.Id_url_three);
        linkView.setClickedListener(this);
        cancelView = (Text) customComponent.findComponentById(ResourceTable.Id_cancel_btn);
        playView = (Text) customComponent.findComponentById(ResourceTable.Id_submit_btn);
        cancelView.setClickedListener(this);
        playView.setClickedListener(this);
        writtenView = (Text) customComponent.findComponentById(ResourceTable.Id_written_v);
        writtenView.setFont(Font.DEFAULT_BOLD);
        desView = (Text) customComponent.findComponentById(ResourceTable.Id_des_v);
        desView.setFont(Font.DEFAULT_BOLD);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_url_one:
                Operation weboperation = new Intent.OperationBuilder()
                    .withUri(Uri.parse(mContext.getString(ResourceTable.String_play_url_1)))
                    .withAction(IntentConstants.ACTION_SEARCH).build();
                intent.setOperation(weboperation);
                mContext.startAbility(intent, 0);
                break;
            case ResourceTable.Id_url_two:
                Operation githuboperation = new Intent.OperationBuilder()
                    .withUri(Uri.parse(mContext.getString(ResourceTable.String_play_url_2)))
                    .withAction(IntentConstants.ACTION_SEARCH).build();
                intent.setOperation(githuboperation);
                mContext.startAbility(intent, 0);
                break;
            case ResourceTable.Id_url_three:
                Operation operation = new Intent.OperationBuilder()
                    .withUri(Uri.parse(mContext.getString(ResourceTable.String_play_url_3)))
                    .withAction(IntentConstants.ACTION_SEARCH).build();
                intent.setOperation(operation);
                mContext.startAbility(intent, 0);
                break;
            case ResourceTable.Id_cancel_btn:
                hide();
                break;
            case ResourceTable.Id_submit_btn:
                Operation playUrl = new Intent.OperationBuilder()
                    .withUri(Uri.parse(mContext.getString(ResourceTable.String_play_url_4)))
                    .withAction(IntentConstants.ACTION_SEARCH).build();
                intent.setOperation(playUrl);
                mContext.startAbility(intent, 0);
                break;
            default:
                break;
        }
    }
}
